 #include "MCU.h"
 #include "clockMan1.h"
 #include "pin_mux.h"
 #include "Tps929120.h"
 #include "Tps929120_PBCfg.h"
 #include "General.h"
 #include "Driver.h"
 #include <string.h>
 #include "SysCommon.h"
 
 /*******************************************************************************
 * Definitions
 ******************************************************************************/


/*******************************************************************************
 * Variables
 ******************************************************************************/
uint8_t g_u8turn_left_cnt = 0;
uint8_t g_u8turn_right_cnt = 0;
uint8_t g_u8Fender_turn_cnt = 0;
uint8_t g_u8P_S_Flag = 0;


/*Functions********************************************************************/
void Pin_PullUp(uint8_t position)
{
#if CURRENT_PROJECT  == PROJECT_trunk
    if (position == 1)
    {
        PINS_DRV_WritePin(PTE, 8, 1);
    }

    if (position == 2)
    {
        PINS_DRV_WritePin(PTB, 5, 1);

    }
#endif

#if ((CURRENT_PROJECT  == PROJECT_Fender_Left)|| (CURRENT_PROJECT  == PROJECT_Fender_Right))
    if (position == 1)
    {
        PINS_DRV_WritePin(PTB, 5, 1);
        // MCU_SetPwmDuty_PTB5(5000);
    }
#endif
}

/*Functions********************************************************************/
void Pin_PullDown(uint8_t position)
{
#if CURRENT_PROJECT  == PROJECT_trunk
    if (position == 1)
    {
        PINS_DRV_WritePin(PTE, 8, 0);
    }

    if (position == 2)
    {
        PINS_DRV_WritePin(PTB, 5, 0);
    }
#endif

#if ((CURRENT_PROJECT  == PROJECT_Fender_Left)|| (CURRENT_PROJECT  == PROJECT_Fender_Right))
    if (position == 1)
    {
        PINS_DRV_WritePin(PTB, 5, 0);
    }
#endif
}

/*Functions********************************************************************/
void PWM_INPUT_Task(void)
{
#if CURRENT_PROJECT  == PROJECT_trunk
    if(LAMB_Status.PO_Left_ADC_Status && LAMB_Status.PO_Right_ADC_Status)
    {
        PWM_Register_Value_Handle_Period(&PWM_Capture_HV, 5);
    }
#endif

#if ((CURRENT_PROJECT  == PROJECT_Fender_Left)|| (CURRENT_PROJECT  == PROJECT_Fender_Right))
    if(LAMB_Fender_Status.PO_ADC_Status)
    {
        PWM_Register_Value_Handle_Period(&PWM_Capture_HV, 5);
    }
#endif


    if(PWM_Capture_HV.mode == 16)
    {
        g_u8P_S_Flag = ON;
        // PWM_Capture_HV.mode = 0;
    }
    

#if CURRENT_PROJECT  == PROJECT_trunk
    if(LAMB_Status.PO_Left_ADC_Status && LAMB_Status.PO_Right_ADC_Status)
    {
        PWM_Mode_Handler_LS();
    }
#endif

#if ((CURRENT_PROJECT  == PROJECT_Fender_Left)|| (CURRENT_PROJECT  == PROJECT_Fender_Right))
    if(LAMB_Fender_Status.PO_ADC_Status)
    {
        PWM_Mode_Handler_LS();
    }
#endif

}

/*Functions********************************************************************/
void Task_LightLogic(void)
{
#if CURRENT_PROJECT  == PROJECT_trunk
    if(LAMB_Status.PO_Left_ADC_Status && LAMB_Status.PO_Right_ADC_Status)
    {
        Task_LightLogic_Handler();
    }
#endif

#if ((CURRENT_PROJECT  == PROJECT_Fender_Left)|| (CURRENT_PROJECT  == PROJECT_Fender_Right))
    if(LAMB_Fender_Status.PO_ADC_Status)
    {
        Task_LightLogic_Handler();
    }
#endif
}

/*Functions********************************************************************/
void Task_ProcessAnimaton(void)
{
    Task_ProcessAnmStep();
}

/*Functions********************************************************************/
void Task_LightGeneral(void)
{
    HALL_50ms();
}

/*Functions********************************************************************/
void Task_ADCUpdate(void)
{
    Clear_PWM_InputCapture_OV_C();
    ADC_Update();
}

/*Functions********************************************************************/
void Task_LEDPWMUpdate(void)
{
    Light_Update();
}

/*Functions********************************************************************/
void Turn_Task(void)
{

#if CURRENT_PROJECT  == PROJECT_trunk
    if(LAMB_Status.Swipe_Turn_Signal == 1)			//turn flow on
    {
        if(LAMB_Status.DI_Left_ADC_Count_H > 1)
        {
            DI_FlowOn(DEV_LEFT, g_u8turn_left_cnt);
            if(g_u8turn_left_cnt < TRUNK_TURN_LEFT_CNT)
            {
                g_u8turn_left_cnt++;
            }
        }


        if(LAMB_Status.DI_Right_ADC_Count_H > 1)
        {
            DI_FlowOn(DEV_RIGHT, g_u8turn_right_cnt);
            if(g_u8turn_right_cnt < TRUNK_TURN_RIGHT_CNT)
            {
                g_u8turn_right_cnt++;
            }
        }


        if((LAMB_Status.PO_Left_ADC_Count_H) || (LAMB_Status.PO_Right_ADC_Count_H))
        {
            if(LAMB_Status.DI_Left_ADC_Count_H == 0)
            {
                DI_Off(DEV_LEFT);
                g_u8turn_left_cnt = 0;
            }
            if(LAMB_Status.DI_Right_ADC_Count_H == 0)
            {
                DI_Off(DEV_RIGHT);
                g_u8turn_right_cnt = 0;
            }
        }
        else
        {
            if(LAMB_Status.DI_Left_ADC_Count_H == 0)
            {
                DI_Off(DEV_LEFT);
                g_u8turn_left_cnt = 0;
            }
            if(LAMB_Status.DI_Right_ADC_Count_H == 0)
            {
                DI_Off(DEV_RIGHT);
                g_u8turn_right_cnt = 0;
            }
        }
    }
    else
    {
        g_u8turn_left_cnt = 0;
        g_u8turn_right_cnt = 0;

        if(LAMB_Status.DI_Left_ADC_Count_H > 1)		// turn on
        {
            DI_On(DEV_LEFT);
            LAMB_Status.DI_Left_ADC_Count_H = 1;
        }


        if(LAMB_Status.DI_Right_ADC_Count_H > 1)
        {
            DI_On(DEV_RIGHT);
            LAMB_Status.DI_Right_ADC_Count_H = 1;
        }


        if((LAMB_Status.PO_Left_ADC_Count_H) || (LAMB_Status.PO_Right_ADC_Count_H))
        {
            if(LAMB_Status.DI_Left_ADC_Count_H == 0)
            {
                DI_Off(DEV_LEFT);
            }
            if(LAMB_Status.DI_Right_ADC_Count_H == 0)
            {
                DI_Off(DEV_RIGHT);
            }
        }
        else
        {
            if(LAMB_Status.DI_Left_ADC_Count_H == 0)
            {
                DI_Off(DEV_LEFT);
            }
            if(LAMB_Status.DI_Right_ADC_Count_H == 0)
            {
                DI_Off(DEV_RIGHT);
            }
        }
    }
#endif

#if ((CURRENT_PROJECT  == PROJECT_Fender_Left)|| (CURRENT_PROJECT  == PROJECT_Fender_Right))
    if(LAMB_Fender_Status.Swipe_Turn_Signal == 1)	
    {
        if(LAMB_Fender_Status.DI_ADC_Count_H > 1)
        {
            DI_ALight_FlowOn(g_u8Fender_turn_cnt);
            if(g_u8Fender_turn_cnt < FENDER_TURN_CNT)
            {
                g_u8Fender_turn_cnt++;
            }
        }
        else
        {
            if(LAMB_Status.DI_Left_ADC_Count_H == 0)
            {
                DI_ALight_Off();
                g_u8Fender_turn_cnt = 0;
            }
        }
    }
    else
    {
        g_u8Fender_turn_cnt = 0;

        if(LAMB_Fender_Status.DI_ADC_Count_H > 1)
        {
            DI_ALight_On();
        }
        else
        {
            if(LAMB_Status.DI_Left_ADC_Count_H == 0)
            {
                DI_ALight_Off();
            }
        }
    }
#endif
}

/*Functions********************************************************************/
void EEPROM_Task(void)
{

}


/*Functions********************************************************************/
void Task_TpsCheck()
{
    static uint8_t one_flag= 1;

    if(one_flag == 1)
    {
        one_flag = 0;
        Pin_PullUp(1);
        Pin_PullUp(2);
    }
    else
    {
#if CURRENT_PROJECT  == PROJECT_trunk
        if((LAMB_Status.DI_Left_ADC_Count_H > 1) && (PWM_Capture_HV.mode == 0))
        {
            uint8_t u8RegData = 0xFF;
            Tps_ReadOneRegBlocking(TPS_CHIP_ADD_14, REG_ADD_FLAG0, &u8RegData);

            Judge_Diag_Data(1);
            Process_Diag_Result(1);
        }
        
        if((LAMB_Status.DI_Right_ADC_Count_H > 1) && (PWM_Capture_HV.mode == 0))
        {
            uint8_t u8RegData = 0xFF;
            Tps_ReadOneRegBlocking(TPS_CHIP_ADD_15, REG_ADD_FLAG0, &u8RegData);

            Judge_Diag_Data(2);
            Process_Diag_Result(2);
        }

        if(LAMB_Status.DI_Left_ADC_Count_H == 0)
        {
            g_u8Left_status = 0;
            g_u8Left_g_u8Board_lost_status = 0;
        }

        if(LAMB_Status.DI_Right_ADC_Count_H == 0)
        {
            g_u8Right_status = 0;
            g_u8Right_g_u8Board_lost_status = 0;
        }
    
#endif

#if ((CURRENT_PROJECT  == PROJECT_Fender_Left)|| (CURRENT_PROJECT  == PROJECT_Fender_Right))
        if((LAMB_Fender_Status.DI_ADC_Count_H > 1) && (PWM_Capture_HV.mode == 0))
        {
            uint8_t u8RegData = 0xFF;
            Tps_ReadOneRegBlocking(TPS_CHIP_ADD_2, REG_ADD_FLAG0, &u8RegData);

            Judge_Diag_Data(1);
            Process_Diag_Result(1);
        }

        if(LAMB_Fender_Status.DI_ADC_Count_H == 0)
        {
            g_u8Turn_status = 0;
            g_u8Board_lost_status = 0;
        }
#endif
    }
}
 
 


 